package com.nervenets.general.entity;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.nervenets.general.enumeration.MessageImportanceLevel;
import com.nervenets.general.utils.JodaUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Map;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class ResponseResult<T> implements Serializable {
    @ApiModelProperty(value = "状态码：默认200、常规错误400、登录失效401")
    @Builder.Default
    private int code = HttpStatus.OK.value();
    @ApiModelProperty(value = "数据", notes = "原始数据")
    private T data;
    @ApiModelProperty(value = "提示消息")
    private String message;
    @ApiModelProperty(value = "消息重要类型")
    @Builder.Default
    private MessageImportanceLevel level = MessageImportanceLevel.normal;
    @ApiModelProperty(value = "服务器时间")
    private long t = JodaUtils.getTimestamp();
    @ApiModelProperty(value = "附加信息")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Map<String, ?> extra;

    public ResponseResult(int code) {
        this(code, null, MessageImportanceLevel.normal);
    }

    public ResponseResult(int code, String message) {
        this(code, null, message);
    }

    public ResponseResult(int code, T data) {
        this(code, data, null);
    }

    public ResponseResult(int code, T data, String message) {
        this(code, data, message, MessageImportanceLevel.normal, JodaUtils.getTimestamp(), null);
    }

    public ResponseResult(int code, String message, MessageImportanceLevel level) {
        this.code = code;
        this.message = message;
        this.level = level;
    }

    public static <T> ResponseResult<T> failed() {
        return new ResponseResult<>(HttpStatus.BAD_REQUEST.value());
    }

    public static <T> ResponseResult<T> failed(String message) {
        return new ResponseResult<>(HttpStatus.BAD_REQUEST.value(), message);
    }

    public static <T> ResponseResult<T> failed(int code, String message) {
        return new ResponseResult<>(code, message);
    }

    public static <T> ResponseResult<T> failed(int code, String message, MessageImportanceLevel level) {
        return new ResponseResult<>(code, message, level);
    }

    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(HttpStatus.OK.value());
    }

    public static <T> ResponseResult<T> success(String message) {
        return new ResponseResult<>(HttpStatus.OK.value(), message);
    }

}
